<?php
    error_reporting(E_ALL || ~E_DEPRECATED || ~E_STRICT || ~E_NOTICE || ~E_WARNING || ~E_CORE_WARNING);
//取JsonString、Picture
    $data='';
    $data=file_get_contents('php://input');
    file_put_contents("test.log","\n aJson: ".$data." line ".__LINE__."\n", FILE_APPEND); 	

 /*    $data='';
    $data=@$_POST["anjianJson"];
	file_put_contents("test.log","\n anjianJson: ".$data." line ".__LINE__."\n", FILE_APPEND);*/
    if($data  && $data!=='')
    {
    	$arecord=json_decode($data,true);

    	if($arecord)
		{
			if($arecord["realtime_type"]==="2" && $arecord["device_id"])
			{
				file_put_contents("test.log","realtime_type: ".$arecord["realtime_type"]."device_id: ".$arecord["device_id"]." LINE ".__LINE__."\n", FILE_APPEND);
				$ajshijianwenjian = fopen("./dwajshijian.txt", "r") or die("Unable to read file!");
				$wjshijian = fread($ajshijianwenjian,filesize("./dwajshijian.txt"));
				fclose($ajshijianwenjian);
				file_put_contents("test.log","wjshijian: ".$wjshijian." LINE ".__LINE__."\n", FILE_APPEND);
				$accesstime=date("Y-m-d H:i:s");
				file_put_contents("test.log","accesstime: ".$accesstime." LINE ".__LINE__."\n", FILE_APPEND);
				if($wjshijian)
				{
					$chazhi = strtotime($accesstime) - strtotime($wjshijian);
					if($chazhi<20)
					{
						file_put_contents("test.log","chazhi ".$chazhi."  accesstime ".strtotime($accesstime)."  wjshijian ".strtotime($wjshijian)." LINE ".__LINE__."\n", FILE_APPEND);
						return;
					}
				}else
				{
					$ajshijianwenjian = fopen("./dwajshijian.txt", "w") or die("Unable to open renliantongzhiwenjian!");
					file_put_contents("test.log","dwajshijian.txt "." LINE ".__LINE__."\n", FILE_APPEND);
					fwrite($ajshijianwenjian, $accesstime);
					fclose($ajshijianwenjian);
				}
			
				$myconn=dwopendb();
				$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$arecord["device_id"]."' AND entrance='in';";
				$result=mysql_query($strSql,$myconn);
				$zuijingjilu = mysql_fetch_array($result);
				mysql_free_result($result);
				file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

				$strSql = "SELECT tkid,controlerNum,cardnum,gatenum,CreatedTime,gonghao FROM accessrecord WHERE tkid =  $zuijingjilu[0];";
				$result=mysql_query($strSql,$myconn);
				$accessjieguoji = mysql_fetch_array($result);
				mysql_free_result($result);
				file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

				$strSql = "SELECT departmentID,staffnum,departmentname,staffname FROM staffbasesetting WHERE staffnum=$accessjieguoji[5];";
				$result=mysql_query($strSql,$myconn);
				$renyuanjieguoji = mysql_fetch_array($result);
				mysql_free_result($result);
				file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
//				anjianbaojing("access",$zuijingjilu,$renyuanjieguoji,$accessjieguoji,$accesstime,$myconn)
				$accesstime=date("Y-m-d H:i:s");
				if ($accessjieguoji[3]=="0")
				{
					if($accessjieguoji[1]=="fxk1")
					{
						$gatenum="2";
						$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi0.txt";
					}else
					{
						$gatenum="3";
						$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi2.txt";  //zao jing 2 zhaji de jilu
					}
				}else if ($accessjieguoji[3]=="2")
				{
					$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi3.txt";  //zao jing 3 zhaji de jilu
					$gatenum="1";
				}else
				{
					chucuoaj("out",$renyuanjieguoji,$accessjieguoji,$accesstime,$myconn);
					file_put_contents("test.log", "chucuo  LINE:".__LINE__."\n", FILE_APPEND);
					return;
				}
				 
				$cmd =  "{\"result\":\"OK\",\"pass\":\"access\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp03\",\"shipin\":\"sp03\",\"accesstime\":\"$accesstime\",\"gatenum\":\"$gatenum\"}";
				echo $cmd;
				file_put_contents("test.log", "cmd =".$cmd." LINE:".__LINE__."\n", FILE_APPEND);	
				xierurenlianwenjian($accessjieguoji[1],$accessjieguoji[3],$renyuanjieguoji[1],$accessjieguoji[4],"jieshu",$zuijingjilu[0],$dwrenlianwenjianlujing,$myconn);
				file_put_contents("test.log", "dwrenlianwenjianlujing =".$dwrenlianwenjianlujing." LINE:".__LINE__."\n", FILE_APPEND);	
				$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren,ajjieguo) VALUES('$accessjieguoji[1]','$accessjieguoji[2]','$gatenum','$accesstime','access','out','$renyuanjieguoji[1]','wu',1);";
				$result=mysql_query($strSql,$myconn);
				@mysql_free_result($result);
				dwclosedb();
				file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

				$ajshijianwenjian = fopen("./dwajshijian.txt", "w") or die("Unable to open renliantongzhiwenjian!");
				//file_put_contents("test.log"," dwrenlianwenjianlujing ".$dwrenlianwenjianlujing." LINE ".__LINE__."\n", FILE_APPEND);
				fwrite($ajshijianwenjian, $accesstime);
				fclose($ajshijianwenjian);
				return;
			}
		}
    }

    $data='';
    $accessrecord='';
	$data=@$_POST["JsonString"];
//    $data=$_GET['action'];
	file_put_contents("test.log","\n JsonString: ".$data." line ".__LINE__."\n", FILE_APPEND); 	
	if($data  && $data!=='')
    {
    	$accessrecord=json_decode($data,true);
		if(!$accessrecord)
		{
		    die('Could not connect: '.json_last_error()." line ".__LINE__."\n");
	//		$string = "Could not connect: ".json_last_error()." line ".__LINE__."\n";
	        file_put_contents("test.log", "Could not connect: ".json_last_error()." line ".__LINE__."\n", FILE_APPEND);
	//		echo "{\"res\":\"error\"}";
			echo "{\"result\":\"ERROR\"}";
			return;
		}
    }else
    {
    	file_put_contents("test.log", "data null   line ".__LINE__."\n", FILE_APPEND);
	//		echo "{\"res\":\"error\"}";
		echo "{\"result\":\"ERROR\"}";
		return;
    }
    
//    $xrecord=json_decode($data,true);
//	if($xrecord)
//	{
	$xrecord = $accessrecord;
    $string = "\n 0 ".$xrecord["device_id"]." 1 ".$xrecord["type"]." 2 ".$xrecord["teyp"]. " 3 ".$xrecord["qrcode"] ." 4 ".$xrecord["filename"]." 5 ".$xrecord["Tail"]." line ".__LINE__."\n";
	file_put_contents("test.log", $string, FILE_APPEND);
	if($xrecord["teyp"] ==="Thsd5030")
	{
/*			if($xrecord["device_id"]==="DAWU")
		{
			$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$xrecord["device_id"]."' AND gatenum='0'";
			$result=mysql_query($strSql,$myconn);
			$zuidajilu = mysql_fetch_array($result);
			mysql_free_result($result);
			$strSql = "UPDATE accessrecord SET xjieguo = '".$xrecord["qrcode"]."' WHERE tkid = '".$$zuidajilu[0]."'";
			$result=mysql_query($strSql,$myconn);
			mysql_free_result($result);
//			}else 
//			{*/
		$xjsonchuan = duchuxwenjian();
		$xrecord = json_decode($xjsonchuan,true);

//		if($xrecord["renliangshibie"]==="kaishi")
//		{
/*			$myconn=dwopendb();
			$strSql = "UPDATE accessrecord SET xjieguo = '".$xrecord["qrcode"]."' WHERE tkid = '".$xrecord["tkid"]."'";
			$result=mysql_query($strSql,$myconn);
			mysql_free_result($result);
			dwclosedb($myconn);
//			xieruxwenjian($xrecord["shebeihao"],$xrecord["menghao"],$xrecord["shuakashijian"],"jieshu",$xrecord["tkid"]);
			file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);*/
//		}				
//		}

		$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = 'fxk2' AND accessstat='access';";
		$zuijingjilu = mysql_fetch_array($result);
		mysql_free_result($result);
		file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

		$strSql = "SELECT tkid,controlerNum,cardnum,gatenum,CreatedTime,gonghao FROM accessrecord WHERE tkid =  $zuijingjilu[0];";
		$accessjieguoji = mysql_fetch_array($result);
		mysql_free_result($result);
		file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

		$strSql = "SELECT departmentID,staffnum,departmentname,staffname FROM staffbasesetting WHERE staffnum=$accessjieguoji[5];";
		$result=mysql_query($strSql,$myconn);
		$renyuanjieguoji = mysql_fetch_array($result);
		mysql_free_result($result);
		file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
//				anjianbaojing("access",$zuijingjilu,$renyuanjieguoji,$accessjieguoji,$accesstime,$myconn)
		if ($accessjieguoji[3]=="0")
		{
			$gatenum="2";
			$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi2.txt";  //zao jing 2 zhaji de jilu
		}else if ($accessjieguoji[3]=="2")
		{
			$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi3.txt";  //zao jing 3 zhaji de jilu
			$gatenum="1";
		}
		$accesstime=date("Y-m-d H:i:s");
		$cmd="{\"dwclient\":\"$renyuanjieguoji[1]\",\"dwdevice\":\"fxk2\",\"dwcmd\":\"xbaojing\",\"staffname\":\"$renyuanjieguoji[3]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"gatenum\":\"$gatenum\",\"shibieshijian\":\"$accesstime\"}";
		dwsendtoserver($cmd,$myconn);
		file_put_contents("test.log", "cmd =".$cmd." LINE:".__LINE__."\n", FILE_APPEND);

		xierurenlianwenjian($accessjieguoji[1],$accessjieguoji[3],$renyuanjieguoji[1],$accessjieguoji[4],"jieshu",$zuijingjilu,$dwrenlianwenjianlujing,$myconn);

		$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('$accessjieguoji[1]','$accessjieguoji[2]','$gatenum','accesstime','access','out','$renyuanjieguoji[1]','wu');";
		$result=mysql_query($strSql,$myconn);
		@mysql_free_result($result);
		dwclosedb();
		file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

		echo "{\"msg\":\"OK\",\"recode\":\"0\"}";						
		
		$tempFile = $_FILES['imagefile']['tmp_name'];
		$Filesize = $_FILES['imagefile']['size'];
		$targetPath = dirname(__FILE__). "\\xpictures\\";
		if($Filesize) xieruxtupian($Filesize,$targetPath,$xrecord["filename"]);
		
		return;
	}
//	}  
	$string = "\n 0 ".$accessrecord["device_id"]." 1 ".$accessrecord["realtime_type"]." 2 ".$accessrecord["idcard"]. " 3 ".$accessrecord["gate"] ." 4 ".$accessrecord["time"]." line ".__LINE__."\n";
	file_put_contents("test.log", $string, FILE_APPEND);
	
//	$access=json_decode($data,true);
	
	if($accessrecord["gate"]===""||($accessrecord["device_id"]!=="fxk1" && $accessrecord["device_id"]!=="fxk2") ||$accessrecord["time"]===""||$accessrecord["idcard"]==="")
	{
		echo "{\"result\":\"ERROR\"}";
		return;
	}
	if($accessrecord["accessstat"]==="")
	{
		$accessrecord["accessstat"]="wait";
	}
	if($accessrecord["entrance"]==="")
	{
		$accessrecord["entrance"]="out";
	}

	$tempFile = $_FILES['Picture']['tmp_name'];
	$Filesize = $_FILES['Picture']['size'];
	$targetPath = dirname(__FILE__). "\\pictures\\";

	$accesstime=strftime('%Y-%m-%d %H:%M:%S',strtotime($accessrecord["time"]));
//	file_put_contents("test.log","\n tempFile: ".$tempFile."   Filesize  ".$Filesize."\n", FILE_APPEND);

	$myconn=dwopendb();

	$strSql = "SELECT MAX(rowid) FROM staffbasesetting WHERE cardnum='".$accessrecord["idcard"]."';";
	$result=mysql_query($strSql,$myconn);
	$renyuanjieguoji = mysql_fetch_array($result);
	mysql_free_result($result);
	file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

	$strSql = "SELECT zhaneiyouren FROM accessrecord WHERE tkid= (SELECT MAX(tkid) FROM accessrecord);";
	$result=mysql_query($strSql,$myconn);
	$zhaneiyouren = mysql_fetch_array($result);                                   //zhanei sifou youren  
	mysql_free_result($result);
	file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

	if(!$renyuanjieguoji[0] || $renyuanjieguoji[0]=="NULL"  )             //feishouquan renyuan
	{
		feishouquanhuoyouren('',$renyuanjieguoji,$accessrecord,$accesstime,$zhaneiyouren[0],$myconn);
		file_put_contents("test.log", "zhaneiyouren  LINE:".__LINE__."\n", FILE_APPEND);

/*		sleep(2);
		$msg="{\"dwdevice\":\"".$accessrecord["device_id"]."\",\"trans_id\":\"".uuid()."\",\"cmd_code\":\"DW_OPENCLOSE\",\"gatenum\":\"".$accessrecord["gate"]."\"}";
		if(dwsendtoserver($msg,$myconn))
		{
//				echo "{\"result\":\"OK\"}";
		}else
		{
//				echo "{\"result\":\"ERROR\"}";
		}
		file_put_contents("test.log", "msg =".$msg." LINE:".__LINE__."\n", FILE_APPEND);*/
		mysql_close($myconn);
		return;
	}

	$strSql = "SELECT zhaneiyouren,gatenum FROM accessrecord WHERE tkid= (SELECT MAX(tkid) FROM accessrecord  WHERE controlerNum='".$accessrecord["device_id"]."');";
	$result=mysql_query($strSql,$myconn);
	$zhaneiyouren = mysql_fetch_array($result);
	mysql_free_result($result);
	file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

	$strSql = "SELECT departmentID,staffnum,departmentname,staffname FROM staffbasesetting WHERE rowid=$renyuanjieguoji[0];";  
	$result=mysql_query($strSql,$myconn);
	$renyuanjieguoji = mysql_fetch_array($result);
	mysql_free_result($result);
	file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);

	if($renyuanjieguoji[0]=="999999" and $renyuanjieguoji[2]=="changkaika")
	{
		lingchu($renyuanjieguoji,$accessrecord,$accesstime,$myconn);
		mysql_close($myconn);
		file_put_contents("test.log", "lingchu  LINE:".__LINE__."\n", FILE_APPEND);
		return;
	}

	if($accessrecord["device_id"]==="fxk1")
	{
		$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi0.txt";
		if($accessrecord["gate"]==="0")                                     //jingru
		{
			if($zhaneiyouren[0]==="you")                                                         //zhanei youren
			{
				feishouquanhuoyouren("vip",$renyuanjieguoji,$accessrecord,$accesstime,$zhaneiyouren[0],$myconn);
				mysql_close($myconn);
				file_put_contents("test.log", "zhaneiyouren  LINE:".__LINE__."\n", FILE_APPEND);
				return;
			}
			$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."' AND cardnum='".$accessrecord["idcard"]."'";
			$result=mysql_query($strSql,$myconn);
			$zuidajilu = mysql_fetch_array($result);
			mysql_free_result($result);
			file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
			if(!$zuidajilu[0] || $zuidajilu[0]=="NULL")               //shouci jingru zhege zhaji
			{
				huanyinguanglin("in",$dwrenlianwenjianlujing,"zhengzhuan","",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				mysql_close($myconn);
				file_put_contents("test.log", "huanyinguanglin  LINE:".__LINE__."\n", FILE_APPEND);
				return;
			}
		    $strSql = "SELECT entrance FROM accessrecord WHERE tkid=$zuidajilu[0];";
			$result=mysql_query($strSql,$myconn);
			$ruzhazhuangtai = mysql_fetch_array($result);
			mysql_free_result($result);
			file_put_contents("test.log", "strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
			if($ruzhazhuangtai[0]!=="in")               //buzai  zhaji nei
			{
				huanyinguanglin("in",$dwrenlianwenjianlujing,"zhengzhuan","",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "huanyinguanglin  LINE:".__LINE__."\n", FILE_APPEND);
			}else
			{
				chucuo("out",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "chucuo  LINE:".__LINE__."\n", FILE_APPEND);
			}
			mysql_close($myconn);
			return;
		}
/*		$renliantongzhiwenjian = fopen($dwrenlianwenjianlujing, "r") or die("Unable to read file!");
		$jsonduixiang = fread($renliantongzhiwenjian,filesize("$dwrenlianwenjianlujing"));
		fclose($renliantongzhiwenjian);
		file_put_contents("test.log", "jsonduixiang:".$jsonduixiang."  LINE:".__LINE__."\n", FILE_APPEND);
		$jsonchuan = json_decode($jsonduixiang,true);
		$rljilu = $jsonchuan["tkid"];
		$rlgonghao = $jsonchuan["gonghao"];

		if(!$rljilu || $rljilu=="NULL" || $rlgonghao!="$renyuanjieguoji[1]")    //chucuo     
		{
			chucuo("in",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
			mysql_close($myconn);
			file_put_contents("test.log", "chucuo rljilu:".$rljilu." rlgonghao:".$rlgonghao."  LINE:".__LINE__."\n", FILE_APPEND);
			return;
		}
		$strSql = "SELECT entrance,ajjieguo,rljieguo FROM accessrecord WHERE tkid=$rljilu;";
		$result=mysql_query($strSql,$myconn);
		$ruzhazhuangtai = mysql_fetch_array($result);
		mysql_free_result($result);

		if($accessrecord["gate"]==="2")                           //tuichu
		{
			if($ruzhazhuangtai[1]==="0" && $ruzhazhuangtai[2]==="0")            //yinggai cong lingyige zhakou chu
			{
				lingchu($renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "lingchu  LINE:".__LINE__."\n", FILE_APPEND);
			}else if($ruzhazhuangtai[1]==="1") //anjian shibai tuichu
			{
				anjianbaojing("access",$rljilu,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "anjianbaojing  LINE:".__LINE__."\n", FILE_APPEND);
			}else if($ruzhazhuangtai[2]==="1") //renliang shibie shibai tuichu
			{
				 rlbaojing("access",$rljilu,$dwrenlianwenjianlujing,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				 file_put_contents("test.log", " rlbaojing  LINE:".__LINE__."\n", FILE_APPEND);
			}else
			{
				chucuo("in",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "chucuo  LINE:".__LINE__."\n", FILE_APPEND);
			}	
		}else if($accessrecord["gate"]==="1")                           //tongguo
		{
			if( $ruzhazhuangtai[1]==="0" && $ruzhazhuangtai[2]==="0")
			{
				huanyinguanglin("out",$dwrenlianwenjianlujing,"zhengzhuan",$rljilu,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "huanyinguanglin  LINE:".__LINE__."\n", FILE_APPEND);
			}else if($ruzhazhuangtai[1]==="1" )	
			{
				anjianbaojing("reject",$rljilu,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "anjianbaojing  LINE:".__LINE__."\n", FILE_APPEND);
			}else if( $ruzhazhuangtai[2]==="1")	
			{
				rlbaojing("reject",$rljilu,$dwrenlianwenjianlujing,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "rlbaojing  LINE:".__LINE__."\n", FILE_APPEND);
			}else
			{
				chucuo("in",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "chucuo  LINE:".__LINE__."\n", FILE_APPEND);
			}
		}*/
	}
	else
	{		
		if($renyuanjieguoji[0]!=="vip")
		{
			feishouquanhuoyouren("vip",$renyuanjieguoji,$accessrecord,$accesstime,$zhaneiyouren[0],$myconn);
			mysql_close($myconn);
			file_put_contents("test.log", "feishouquanhuoyouren  LINE:".__LINE__."\n", FILE_APPEND);
			return;
		}
		if($zhaneiyouren[0]==="you")                                                                           //zhanei youren
		{
			feishouquanhuoyouren("vip",$renyuanjieguoji,$accessrecord,$accesstime,$zhaneiyouren[0],$myconn);
			mysql_close($myconn);
			file_put_contents("test.log", "zhaneiyouren  LINE:".__LINE__."\n", FILE_APPEND);
			return;
		}
		
		$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."' AND cardnum='".$accessrecord["idcard"]."'";
		$result=mysql_query($strSql,$myconn);
		$zuidajilu = mysql_fetch_array($result);
		mysql_free_result($result);
		
	/*	$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."' AND cardnum='".$accessrecord["idcard"]."' AND gatenum='2')";
		$result=mysql_query($strSql,$myconn);
		$zuidajilu2 = mysql_fetch_array($result);
		mysql_free_result($result);*/

		if(!$zuidajilu[0] || $zuidajilu[0]=="NULL")                                                         //shouci jingru zhege zhaji
		{		
			if($accessrecord["gate"]==="0")
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi2.txt";  //zao jing 2 zhaji de jilu
//				$msg="{\"HeaderStx\":\"EB\", \"device_id\":\"DAWU2\" ,\"trans_id\":\"".uuid()."\",\"PacketType\":\"02\", \"Data\" : \"02\", \"EndText\" : \"03\"}";
				huanyinguanglin("in",$dwrenlianwenjianlujing,"zhengzhuan","",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "huanyinguanglin  LINE:".__LINE__."\n", FILE_APPEND);
//				xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"kaishi",$zuidajilu0[0]);
			}else if($accessrecord["gate"]==="2")
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi3.txt";  //zao jing 3 zhaji de jilu
//				$msg="{\"HeaderStx\":\"EB\", \"device_id\":\"DAWU2\" ,\"trans_id\":\"".uuid()."\",\"PacketType\":\"02\", \"Data\" : \"03\", \"EndText\" : \"03\"}";
				huanyinguanglin("in",$dwrenlianwenjianlujing,"fanzhuan","",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "huanyinguanglin  LINE:".__LINE__."\n", FILE_APPEND);
//				xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"kaishi",$zuidajilu2[0]);
			}	
			mysql_close($myconn);
			return;
//			if($Filesize) xieruzaopian($Filesize,$myconn);
		}

		$ruzhazhuangtai0 = array();
		$ruzhazhuangtai2 = array();
		if($zuidajilu[0])
		{
			$strSql = "SELECT entrance FROM accessrecord WHERE tkid=$zuidajilu[0];";
			$result=mysql_query($strSql,$myconn);
			$ruzhazhuangtai = mysql_fetch_array($result);
			mysql_free_result($result);
		}
/*		else if($zuidajilu2[0])
		{
			$strSql = "SELECT entrance FROM accessrecord WHERE tkid=$zuidajilu2[0];";
			$result=mysql_query($strSql,$myconn);
			$ruzhazhuangtai2 = mysql_fetch_array($result);
			mysql_free_result($result);	
		}*/

//		if((($accessrecord["gate"]==="0") && ($ruzhazhuangtai0[0]!=="in")) or (($accessrecord["gate"]==="2") && ($ruzhazhuangtai2[0]!=="in"))) 
		if ($ruzhazhuangtai[0]!=="in")                                                                                                          //buzai  zhaji nei
		{
			if($accessrecord["gate"]==="0")
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi2.txt";          //zhixiang 0 zhaji de jilu
				huanyinguanglin("in",$dwrenlianwenjianlujing,"zhengzhuan","",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "huanyinguanglin  LINE:".__LINE__."\n", FILE_APPEND);
//				xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"kaishi",$zuidajilu0[0]);
			}else if($accessrecord["gate"]==="2")
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi3.txt";          //zhixiang 2 zhaji de jilu
				huanyinguanglin("in",$dwrenlianwenjianlujing,"fanzhuan","",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				file_put_contents("test.log", "huanyinguanglin  LINE:".__LINE__."\n", FILE_APPEND);
//				xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"kaishi",$zuidajilu2[0]);
			}
			mysql_close($myconn);			
			return;
		}else
		{
			chucuo("out",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
			mysql_close($myconn);
			file_put_contents("test.log", "chucuo  LINE:".__LINE__."\n", FILE_APPEND);		
			return;
		}
			
/*		$dwrenlianwenjianlujing2 = "./dwrlwj/dwrenliantongzhi3.txt";              //zhixiang 2 zhaji de jilu
		$renliantongzhiwenjian = fopen($dwrenlianwenjianlujing2, "r") or die("Unable to read file!");
		$jsonduixiang = fread($renliantongzhiwenjian,filesize($dwrenlianwenjianlujing2));
		fclose($renliantongzhiwenjian);
		$jsonchuan = json_decode($jsonduixiang,true);
		$rlhangjilu2 = $jsonchuan["tkid"];
		$rlgonghao2 = $jsonchuan["gonghao"];

		$dwrenlianwenjianlujing0 = "./dwrlwj/dwrenliantongzhi2.txt";             //zhixiang 0 zhaji de jilu
		$renliantongzhiwenjian = fopen($dwrenlianwenjianlujing0, "r") or die("Unable to read file!");
		$jsonduixiang = fread($renliantongzhiwenjian,filesize($dwrenlianwenjianlujing0));
		fclose($renliantongzhiwenjian);
		$jsonchuan = json_decode($jsonduixiang,true);
		$rlhangjilu0 = $jsonchuan["tkid"];
		$rlgonghao0 = $jsonchuan["gonghao"];

		if((($accessrecord["gate"]==="1") && (!$rlhangjilu0 || $rlhangjilu0=="NULL" || $rlgonghao0!= "$renyuanjieguoji[1]")) or (($accessrecord["gate"]==="3")  && (!$rlhangjilu2 || $rlhangjilu2=="NULL" || $rlgonghao2!= "$renyuanjieguoji[1]")))  //chucuo     
		{
			chucuo("in",$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
//			if($Filesize) xieruzaopian($Filesize,$myconn)
		}

		$ruzhazhuangtai0 = array();
		$ruzhazhuangtai2 = array();
		if($rlhangjilu0[0] )
		{
			$strSql = "SELECT xjieguo,ajjieguo,rljieguo FROM accessrecord WHERE tkid=rlhangjilu0[0];";
			$result=mysql_query($strSql,$myconn);
			$ruzhazhuangtai0 = mysql_fetch_array($result);
			mysql_free_result($result);
		}
		else if($rlhangjilu2[0] )
		{
			$strSql = "SELECT xjieguo,ajjieguo,rljieguo FROM accessrecord WHERE tkid=$zuidajilu2[0];";
			$result=mysql_query($strSql,$myconn);
			$ruzhazhuangtai2 = mysql_fetch_array($result);
			mysql_free_result($result);	
		}
		
		if($accessrecord["gate"]==="1")          
		{
			if($zhaneiyouren[1]==="0")                                                             //shiwei 0 jing, tongguo
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi2.txt";             //zhixiang 0 zhaji de jilu
				if($ruzhazhuangtai0[1]==="0" && $ruzhazhuangtai0[2]==="0" && $ruzhazhuangtai0[3]==="0")
				{				
					huanyinguanglin("out",$dwrenlianwenjianlujing,"zhengzhuan",$rlhangjilu0[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				}else if($ruzhazhuangtai0[1]!=="0")
				{
					anjianbaojing("reject",$rlhangjilu0[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);
					file_put_contents("test.log", "anjianbaojing  LINE:".__LINE__."\n", FILE_APPEND);
				}else if($ruzhazhuangtai0[2]!=="0")
				{
					rlbaojing("reject",$rlhangjilu0[0],$dwrenlianwenjianlujing,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "rlbaojing  LINE:".__LINE__."\n", FILE_APPEND);	
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);		
				}else if($ruzhazhuangtai0[3]!=="0")
				{
					xbaojing("reject",$rlhangjilu0[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "xbaojing  LINE:".__LINE__."\n", FILE_APPEND);			
				}
			}else                                                                                                 //shiwei 2 jing, tuichu      
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi3.txt";             //zhixiang 2 zhaji de jilu
				if($ruzhazhuangtai2[1]==="0" && $ruzhazhuangtai2[2]==="0" && $ruzhazhuangtai2[3]==="0")
				{
					lingchu($renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				}else if($ruzhazhuangtai2[1]!=="0")
				{
					anjianbaojing("access",$rlhangjilu2[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);
					file_put_contents("test.log", "anjianbaojing  LINE:".__LINE__."\n", FILE_APPEND);
				}else if($ruzhazhuangtai2[2]!=="0")
				{
					rlbaojing("access",$rlhangjilu2[0],$dwrenlianwenjianlujing,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "rlbaojing  LINE:".__LINE__."\n", FILE_APPEND);	
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);		
				}else if($ruzhazhuangtai2[3]!=="0")
				{
					xbaojing("access",$rlhangjilu2[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "xbaojing  LINE:".__LINE__."\n", FILE_APPEND);			
				}
			}
		}else        
		{
			if($zhaneiyouren[1]==="2")                                                             //shiwei 2 jing, tongguo
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi3.txt";             //zhixiang 2 zhaji de jilu
				if($ruzhazhuangtai2[1]==="0" && $ruzhazhuangtai2[2]==="0" && $ruzhazhuangtai2[3]==="0")
				{				
					huanyinguanglin("out",$dwrenlianwenjianlujing,"zhengzhuan",$rlhangjilu2[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				}else if($ruzhazhuangtai2[1]!=="0")
				{
					anjianbaojing("reject",$rlhangjilu2[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);
					file_put_contents("test.log", "anjianbaojing  LINE:".__LINE__."\n", FILE_APPEND);
				}else if($ruzhazhuangtai2[2]!=="0")
				{
					rlbaojing("reject",$rlhangjilu2[0],$dwrenlianwenjianlujing,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "rlbaojing  LINE:".__LINE__."\n", FILE_APPEND);	
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);		
				}else if($ruzhazhuangtai2[3]!=="0")
				{
					xbaojing("reject",$rlhangjilu2[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "xbaojing  LINE:".__LINE__."\n", FILE_APPEND);			
				}
			}else                                                                                                 //shiwei 0 jing, tuichu      
			{
				$dwrenlianwenjianlujing = "./dwrlwj/dwrenliantongzhi2.txt";             //zhixiang 0 zhaji de jilu
				if($ruzhazhuangtai0[1]==="0" && $ruzhazhuangtai0[2]==="0" && $ruzhazhuangtai0[3]==="0")
				{
					lingchu($renyuanjieguoji,$accessrecord,$accesstime,$myconn);
				}else if($ruzhazhuangtai0[1]!=="0")
				{
					anjianbaojing("access",$rlhangjilu0[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);
					file_put_contents("test.log", "anjianbaojing  LINE:".__LINE__."\n", FILE_APPEND);
				}else if($ruzhazhuangtai0[2]!=="0")
				{
					rlbaojing("access",$rlhangjilu0[0],$dwrenlianwenjianlujing,$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "rlbaojing  LINE:".__LINE__."\n", FILE_APPEND);	
//					xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$zuidajilu0[0]);		
				}else if($ruzhazhuangtai0[3]!=="0")
				{
					xbaojing("access",$rlhangjilu0[0],$renyuanjieguoji,$accessrecord,$accesstime,$myconn);
					file_put_contents("test.log", "xbaojing  LINE:".__LINE__."\n", FILE_APPEND);			
				}
			}		
		}*/		
	}

	mysql_close($myconn);
	return;

	function feishouquanhuoyouren($vip,$renyuanjieguoji,$accessrecord,$accesstime,$zhaneiyouren,$myconn)
	{
		if($vip==="vip")
		{
			$cmd="{\"result\":\"OK\",\"pass\":\"reject00\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp05\",\"shipin\":\"sp05\",\"shibieshijian\":\"$accesstime\"}";
			echo $cmd;
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject00','out','$renyuanjieguoji[1]','$zhaneiyouren');";
		}else
		{
			$cmd="{\"result\":\"OK\",\"pass\":\"reject00\",\"userid\":\"NULL\",\"departmentname\":\"NULL\",\"name\":\"NULL\",\"yinpin\":\"yp05\",\"shipin\":\"sp05\",\"shibieshijian\":\"$accesstime\"}";
			echo $cmd;
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject00','out','NULL','".$zhaneiyouren."');";
		}
		
		$result=mysql_query($strSql,$myconn);
		@mysql_free_result($result);
		file_put_contents("test.log", "cmd =".$cmd. "  strSql =".$strSql." LINE:".__LINE__."\n", FILE_APPEND);
		//		if($Filesize) xieruzaopian($Filesize,$myconn);
	}

	function lingchu($renyuanjieguoji,$accessrecord,$accesstime,$myconn)
	{
		echo "{\"result\":\"OK\",\"pass\":\"reject01\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp02\",\"shipin\":\"sp02\",\"anjian\":\"jieshu\"}";
		$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject01','in','$renyuanjieguoji[1]','wu');";
		$result=mysql_query($strSql,$myconn);
		@mysql_free_result($result);
	}
	
	function xbaojing($x,$xljilu,$renyuanjieguoji,$accessrecord,$accesstime,$myconn)
	{	
		if($x==="reject")
		{
			echo "{\"result\":\"OK\",\"pass\":\"reject03\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp04\",\"shipin\":\"sp04\",\"anjian\":\"jieshu\"}";
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject03','$ruzhazhuangtai2[0]','$renyuanjieguoji[1]','$zhaneiyouren[0]');";
			$result=mysql_query($strSql,$myconn);
			mysql_free_result($result);

//			xierurenlianwenjian($accessrecord["device_id"],$accessrecord["gate"],$renyuanjieguoji[1],$accesstime,"jieshu",$xljilu,$dwrenlianwenjianlujing,$myconn);
//			xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$rlhangjilu0);
		}else
		{
			echo "{\"result\":\"OK\",\"pass\":\"access\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp04\",\"shipin\":\"sp04\"}";
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','access','out','$renyuanjieguoji[1]','wu');";
			$result=mysql_query($strSql,$myconn);
			mysql_free_result($result);
		}
//		xierurenlianwenjian($accessrecord["device_id"],$accessrecord["gate"],$renyuanjieguoji[1],$accesstime,"jieshu",$xljilu,$dwrenlianwenjianlujing,$myconn);
//		xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$xljilu);
	}

	function rlbaojing($rl,$rljilu,$dwrenlianwenjianlujing,$renyuanjieguoji,$accessrecord,$accesstime,$myconn)
	{
		if($rl==="reject")
		{
			echo "{\"result\":\"OK\",\"pass\":\"reject03\",\"userid\":\"$renyuanjieguoji[0]\",\"departmentname\":\"$renyuanjieguoji[1]\",\"name\":\"$renyuanjieguoji[2]\",\"yinpin\":\"yp04\",\"shipin\":\"sp04\",\"anjian\":\"jieshu\"}";
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject03','out','$renyuanjieguoji[1]','you');";
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);

			$strSql="UPDATE  accessrecord SET entrance='out' WHERE tkid=$zuidajilu0;"; 
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);
		}else
		{
			echo "{\"result\":\"OK\",\"pass\":\"access\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp04\",\"shipin\":\"sp04\",\"anjian\":\"jieshu\"}";
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','access','out','$renyuanjieguoji[1]','wu');";
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);
		}
		xierurenlianwenjian($accessrecord["device_id"],$accessrecord["gate"],$renyuanjieguoji[1],$accesstime,"jieshu",$rljilu,$dwrenlianwenjianlujing,$myconn);
	}
		
	function anjianbaojing($aj,$rljilu,$renyuanjieguoji,$accessrecord,$accesstime,$myconn)
	{
		if($aj==="reject")
		{
			echo "{\"result\":\"OK\",\"pass\":\"reject02\",\"userid\":\"$renyuanjieguoji[0]\",\"departmentname\":\"$renyuanjieguoji[1]\",\"name\":\"$renyuanjieguoji[2]\",\"yinpin\":\"yp03\",\"shipin\":\"sp03\",\"anjian\":\"jieshu\"}";
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject02','out','$renyuanjieguoji[1]','you');";
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);

	/*				$strSql="UPDATE  accessrecord SET entrance='out' WHERE tkid=$zuidajilu0;"; 
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);*/
		}else
		{
			echo "{\"result\":\"OK\",\"pass\":\"access\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp03\",\"shipin\":\"sp03\",\"anjian\":\"jieshu\"}";
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','access','out','$renyuanjieguoji[1]','wu');";
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);
		}
		xierurenlianwenjian($accessrecord["device_id"],$accessrecord["gate"],$renyuanjieguoji[1],$accesstime,"jieshu",$rljilu,$dwrenlianwenjianlujing,$myconn);
	}

	function chucuoaj($c,$renyuanjieguoji,$accessrecord,$accesstime,$myconn)
	{
		echo "{\"result\":\"OK\",\"pass\":\"reject05\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp07\",\"shipin\":\"sp07\",\"anjian\":\"jieshu\"}";
		if($c==="out")
		{
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord[1]."','".$accessrecord[2]."','".$accessrecord[3]."','".$accessrecord[4]."','reject05','out','$renyuanjieguoji[1]','');";//zhaneiyouren  ????
		}else
		{
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord[1]."','".$accessrecord[2]."','".$accessrecord[2]."','".$accessrecord[4]."','reject05','in','$renyuanjieguoji[1]','');";//zhaneiyouren  ????
		}	
   		$result=mysql_query($strSql,$myconn);
		mysql_free_result($result);
//		if($Filesize) xieruzaopian($Filesize,$myconn);
		file_put_contents("test.log", "strSql ".$strSql." LINE ".__LINE__."\n", FILE_APPEND);
	}

	function chucuo($c,$renyuanjieguoji,$accessrecord,$accesstime,$myconn)
	{
		echo "{\"result\":\"OK\",\"pass\":\"reject05\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp07\",\"shipin\":\"sp07\",\"anjian\":\"jieshu\"}";
		if($c==="out")
		{
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject05','out','$renyuanjieguoji[1]','$zhaneiyouren[0]');";
		}else
		{
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','reject05','in','$renyuanjieguoji[1]','$zhaneiyouren[0]');";
		}	
   		$result=mysql_query($strSql,$myconn);
		mysql_free_result($result);
//		if($Filesize) xieruzaopian($Filesize,$myconn);
		file_put_contents("test.log", "strSql ".$strSql." LINE ".__LINE__."\n", FILE_APPEND);
	}

	function huanyinguanglin($in,$dwrenlianwenjianlujing,$xfangxiang,$rljilu,$renyuanjieguoji,$accessrecord,$accesstime,$myconn)
	{
		if($in==="in")
		{
			echo "{\"result\":\"OK\",\"pass\":\"access\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp01\",\"shipin\":\"sp01\",\"shibieshijian\":\"$accesstime\"}";		
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren,ajjieguo,rljieguo,xjieguo) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','access','in','$renyuanjieguoji[1]','you',0,1,0);";
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);	
			file_put_contents("test.log", "strSql ".$strSql." LINE ".__LINE__."\n", FILE_APPEND);
			$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."' AND cardnum='".$accessrecord["idcard"]."' AND gatenum='".$accessrecord["gate"]."';";
			$result=mysql_query($strSql,$myconn);
			$zuidajilu = mysql_fetch_array($result);           //shangmian gangcharu de jilu  
			@mysql_free_result($result);	
			file_put_contents("test.log", "strSql ".$strSql." LINE ".__LINE__."\n", FILE_APPEND);
			xierurenlianwenjian($accessrecord["device_id"],$accessrecord["gate"],$renyuanjieguoji[1],$accesstime,"kaishi",$zuidajilu[0],"$dwrenlianwenjianlujing",$myconn);
			file_put_contents("test.log", "device_id ".$accessrecord["device_id"]." LINE ".__LINE__."\n", FILE_APPEND);
			if($xfangxiang==="zhengzhuan")
			{
				$msg="{\"HeaderStx\":\"EB\", \"device_id\":\"DAWU\" ,\"trans_id\":\"".uuid()."\",\"PacketType\":\"02\", \"Data\" : \"02\", \"EndText\" : \"03\"}";
			}else
			{
				$msg="{\"HeaderStx\":\"EB\", \"device_id\":\"DAWU\" ,\"trans_id\":\"".uuid()."\",\"PacketType\":\"02\", \"Data\" : \"03\", \"EndText\" : \"03\"}";
			}				
			dwsendtoserver($msg,$myconn);
//			xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"kaishi",$zuidajilu[0]);
//		if($Filesize) xieruzaopian($Filesize,$myconn);
		}
	/*	else
		{
			echo "{\"result\":\"OK\",\"pass\":\"access\",\"userid\":\"$renyuanjieguoji[1]\",\"departmentname\":\"$renyuanjieguoji[2]\",\"name\":\"$renyuanjieguoji[3]\",\"yinpin\":\"yp01\",\"shipin\":\"sp01\",\"anjian\":\"jieshu\"}";
				
			$strSql="INSERT INTO accessrecord(controlerNum,cardnum,gatenum,CreatedTime,accessstat,entrance,gonghao,zhaneiyouren) VALUES('".$accessrecord["device_id"]."','".$accessrecord["idcard"]."','".$accessrecord["gate"]."','".$accesstime."','access','out','$renyuanjieguoji[1]','wu');";
			$result=mysql_query($strSql,$myconn);
			@mysql_free_result($result);		

//				$strSql="UPDATE  accessrecord SET entrance='out' WHERE tkid=$zuidajilu0;"; 
//				$result=mysql_query($strSql,$myconn);
//				@mysql_free_result($result);

			xierurenlianwenjian($accessrecord["device_id"],$accessrecord["gate"],$renyuanjieguoji[1],$accesstime,"jieshu",$rljilu,$dwrenlianwenjianlujing,$myconn);
//			xieruxwenjian($accessrecord["device_id"],$accessrecord["gate"],$accesstime,"jieshu",$rljilu);
		}*/
	}

	function xieruxtupian($Filesize,$targetPath,$targetFile)
	{
		move_uploaded_file($tempFile,$targetPath.$targetFile);
		$string = "\n tempFile:".$tempFile."    targetPath.targetFile:  ".$targetPath.$targetFile." LINE ".__LINE__."\n";
		file_put_contents("test.log", $string, FILE_APPEND);
	}	

	function xieruzaopian($Filesize,$myconn,$accessrecord,$accesstime)
	{
		$strSql = "SELECT MAX(tkid) FROM accessrecord WHERE controlerNum = '".$accessrecord["device_id"]."' AND cardnum='".$accessrecord["idcard"]."' AND gatenum='".$accessrecord["gate"]."' AND CreatedTime='".$accesstime."';";
		$result=mysql_query($strSql,$myconn);
		$dbResult1 = mysql_fetch_array($result);
		@mysql_free_result($result);
		if($dbResult1[0])
		{
			$targetFile =$dbResult1[0].".jpg";			
			move_uploaded_file($tempFile,$targetPath.$targetFile);
	//			$string = "\n tempFile:".$tempFile."    targetPath.targetFile:  ".$targetPath.$targetFile."... \n";
	//			file_put_contents("test.log", $string, FILE_APPEND);
		}
	}	
	
	function xierurenlianwenjian($controlerNum,$gatenum,$gonghao,$accesstime,$renliangshibie,$rlhangjilu,$dwrenlianwenjianlujing,$myconn)
	{
		$strSql = "SELECT CameraRTSP FROM gatesetting WHERE controlerNum = '".$controlerNum."' AND gatenum='".$gatenum."';";
		$result=mysql_query($strSql,$myconn);
		$CameraRTSP = mysql_fetch_array($result);
		file_put_contents("test.log", "strSql ".$strSql." CameraRTSP ".$CameraRTSP[0]." dwrenlianwenjianlujing: ".$dwrenlianwenjianlujing." LINE ".__LINE__."\n", FILE_APPEND);
//		if($CameraRTSP && $CameraRTSP!=("127.0.0.1"))
//		{
			$renliantongzhiwenjian = fopen($dwrenlianwenjianlujing, "w") or die("Unable to open renliantongzhiwenjian!");
			file_put_contents("test.log"," dwrenlianwenjianlujing ".$dwrenlianwenjianlujing." LINE ".__LINE__."\n", FILE_APPEND);
			$renliantongzhixinxi = "{\"shebeihao\":\"".$controlerNum."\",\"gonghao\":\"$gonghao\",\"menghao\":\"".$gatenum."\",\"shexiangtouRTSP\":\"".$CameraRTSP[0]."\",\"shuakashijian\":\"$accesstime\",\"renliangshibie\":\"$renliangshibie\",\"tkid\":\"$rlhangjilu\"}";
			file_put_contents("test.log", "renliantongzhixinxi ".$renliantongzhixinxi." dwrenlianwenjianlujing ".$dwrenlianwenjianlujing." LINE ".__LINE__."\n", FILE_APPEND);
			fwrite($renliantongzhiwenjian, $renliantongzhixinxi);
			fclose($renliantongzhiwenjian);
			file_put_contents("test.log", "renliantongzhixinxi ".$renliantongzhixinxi." dwrenlianwenjianlujing ".$dwrenlianwenjianlujing." LINE ".__LINE__."\n", FILE_APPEND);
//		}	
	}

	function xieruxwenjian($controlerNum,$gatenum,$accesstime,$renliangshibie,$rlhangjilu)
	{
		$xwenjian = fopen("./xwenjian.txt", "w") or die("Unable to open xwenjian!");
		$xxinxi = "{\"shebeihao\":\"".$controlerNum."\",\"menghao\":\"".$gatenum."\",\"shuakashijian\":\"$accesstime\",\"renliangshibie\":\"$renliangshibie\",\"tkid\":\"$rlhangjilu\"}";
		fwrite($xwenjian, $xxinxi);
		fclose($xwenjian);	
	}

	function duchuxwenjian()
	{
		$xwenjian = fopen("./xwenjian.txt", "r") or die("Unable to open xwenjian!");
		$xjsonchuan = fread($xwenjian,filesize("./xwenjian.txt"));
		fclose($xwenjian);
		return $xjsonchuan;
	}

	function dwsendtoserver($msg,$myconn)
	{		
		$strSql="SELECT DISTINCT TcpPORT FROM gatesetting;";
		$result44=mysql_query($strSql,$myconn);
		$dbResult44 = mysql_fetch_array($result44);
		if($dbResult44[0])
		{
			$TCPport = $dbResult44[0];
		}else
		{
			$TCPport = "1119";
		}
		mysql_free_result($result44);

		if( ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false)
		{
			return false;
        }else
		{

		}
		if(socket_connect($sock, '127.0.0.1', $TCPport)===false)
		{
			//socket_clear_error();
			socket_close($sock);
			return false;
		}
		if(socket_sendto($sock, $msg, strlen($msg),0,"127.0.0.1",$TCPport)===false)
		{
			//socket_clear_error();
			socket_close($sock);
			return false;
		}else
		{

		}
		//socket_clear_error();
		socket_close($sock);
		return true;
	}
	
	function dwclosedb($myconn)
	{
		mysql_close($myconn);	
	}

	function dwopendb()
	{
		$myconn=@mysql_connect("localhost","dw","DWdawu-309");
		if(!$myconn)
		{
//			die('Could not connect: '.mysql_error());
			$string = "Can't connect database : ".mysql_error();
			file_put_contents("test.log", $string, FILE_APPEND);
			return false;
		}
		$db_selected=mysql_select_db("dwzuapairenlian",$myconn);
		if(!$db_selected)
		{
//			die("Can't use database dwaccessDB : ".mysql_error());
			$string = "Can't use database : ".mysql_error();
			file_put_contents("test.log", $string, FILE_APPEND);		
			mysql_close($myconn);
			return false;
		}
		return $myconn;
	}
	
    function uuid() 
	{
        if (function_exists('com_create_guid')) 
		{
            return md5(com_create_guid());
        }else 
		{
            mt_srand((double)microtime()*10000);                   //optional for php 4.2.0 and up.
            $charid = md5(uniqid(rand(),true)); 

            return $charid;
        }
    }

	exit(0);
?> 
